/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.webuisubsystem.managedbean.validator;

import assisisw.smartcal.entitysubsystem.User;
import assisisw.smartcal.entitysubsystem.exceptions.user.UserNotRegisteredException;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Francesco
 */
@ManagedBean ( name = "userNameSignInValidator" )
public class UserNameSignInValidator extends AbstractUserNameValidator implements Serializable
{
    
    // Static Fields

    /***/
    private static final FacesMessage usernameNotInMessage = new FacesMessage ( "Sorry, but it appears that your are not registered with this mail: check it or Sign Up !" ) ;
    
    /***/
    private static final FacesMessage usernameNotActivatedMessage = new FacesMessage ( "Sorry, but it appears that you have not activated your account yet. Do it now !" ) ;
    
    // Instance Methods
    
    /***/
    public UserNameSignInValidator () 
    {
        super () ;
    }

    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    protected void specificValidation ( String mail ) throws ValidatorException 
    {
        User user ;
        if ( getUserController().isRegistered ( mail )  )
        {
            try 
            {
                user = getUserController ().getUser ( mail ) ;
                if ( ! user.isActive () )
                    throw new ValidatorException ( usernameNotActivatedMessage ) ;
            }
            catch (UserNotRegisteredException ex) 
            {
                throw new ValidatorException ( usernameNotInMessage ) ;
            }
        }  
        throw new ValidatorException ( usernameNotInMessage ) ;
    }
   
}
